home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / networking / amitcp / httpd.lha / httpd / http_dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-18  |  2.7 KB  |  126 lines

  1. /*
  2.  * http_dir.c: Handles the on-the-fly html index generation
  3.  * 
  4.  * Rob McCool
  5.  * 3/23/93
  6.  * 
  7.  */
  8.  
  9.  
  10. /* httpd.h includes proper directory file */
  11. #include "httpd.h"
  12.  
  13. struct ent {
  14.     char *name;
  15.     struct ent *next;
  16. };
  17.  
  18.  
  19. void output_directories(char **ar,int n,char *name,FILE *fd)
  20. {
  21.     int x,lnum=0;
  22.     char blorf[MAX_STRING_LEN];
  23.  
  24.     if(name[0] == '\0') { 
  25.         name[0] = '/'; name[1] = '\0'; 
  26.     }
  27.     /* aaaaargh Solaris sucks. */
  28.     fflush(fd);
  29.     for(x=0;x<n;x++) {
  30.         if(strcmp(ar[x],".")) {
  31.             fprintf(fd,"<LI> <A NAME=%d HREF=\"",++lnum);
  32.             if(strcmp(ar[x],"..")) {
  33.                 make_full_path(name,ar[x],blorf);
  34.                 escape_url(blorf);
  35.                 fprintf(fd,"%s\">%s</A>",blorf,ar[x]);
  36.             }
  37.             else {
  38.                 make_full_path(name,"..",blorf);
  39.                 getparents(blorf);
  40.                 if(blorf[0] == '\0') {
  41.                     blorf[0] = '/'; blorf[1] = '\0';
  42.                 }
  43.                 escape_url(blorf);
  44.                 fprintf(fd,"%s\">%s</A>",blorf,"Parent Directory");
  45.             }
  46.             fputc(LF,fd);
  47.         }
  48.     }
  49. }    
  50.  
  51. int dsortf(char **s1,char **s2)
  52. {
  53.     return(strcmp(*s1,*s2));
  54. }
  55.  
  56.     
  57. void index_directory(char *name, FILE *fd)
  58. {
  59.     DIR *d;
  60.     struct DIR_TYPE *dstruct;
  61.     int num_ent=0,x;
  62.     struct ent *head,*p,*q;
  63.     char **ar;
  64.     char unmunged_name[MAX_STRING_LEN];
  65.  
  66.     strcpy(unmunged_name,name);
  67.     unmunge_name(unmunged_name);
  68.  
  69.     if(!(d=opendir(name)))
  70.         die(FORBIDDEN,unmunged_name,fd);
  71.  
  72.     set_content_type(".html");
  73.     if(!assbackwards) 
  74.         send_mime_headers(fd);
  75.  
  76.     if(header_only) return;
  77.  
  78. /* Spew HTML preamble */
  79.     fprintf(fd,"<TITLE>Index of %s</TITLE>",unmunged_name);
  80.     fputc(LF,fd);
  81.     fprintf(fd,"<H1>Index of %s</H1>",unmunged_name);
  82.     fputc(LF,fd);
  83.  
  84.     fprintf(fd,"<UL>");
  85.     fputc(LF,fd);
  86.  
  87. /* 
  88.  * Since we don't know how many dir. entries there are, put them into a 
  89.  * linked list and then arrayificate them so qsort can use them. 
  90.  */
  91.     head=NULL;
  92.     while(dstruct=readdir(d)) {
  93.         p=(struct ent *)malloc(sizeof(struct ent));
  94.         p->name=strdup(dstruct->d_name);
  95.         p->next=head;
  96.         head=p;
  97.         num_ent++;
  98.     }
  99.     ar=(char **) malloc(num_ent*sizeof(char *));
  100.     p=head;
  101.     x=0;
  102.     while(p) {
  103.       ar[x++]=p->name;
  104.       p = p->next;
  105.     }
  106.     
  107.     qsort((void *)ar,num_ent,sizeof(char *),
  108. #ifdef ULTRIX_BRAIN_DEATH
  109.           (int (*))dsortf);
  110. #else
  111.           (int (*)(const void *,const void *))dsortf);
  112. #endif
  113.  
  114.           output_directories(ar,num_ent,unmunged_name,fd);
  115.           free(ar);
  116.           q = head;
  117.     while(q) {
  118.         p=q->next;
  119.         free(q->name);
  120.         free(q);
  121.         q=p;
  122.     }
  123.     closedir(d);
  124.     fprintf(fd,"</UL>");
  125. }
  126.